
use crate::service::{crud_service::CrudService, {{table_name}}_service::{{model_name}}Service};

use crate::APPLICATION_CONTEXT;
use axum::{
extract::{Path, Query},
response::IntoResponse,
routing::get,
Json, Router,
};
use cassie_common::{error::Error, RespVO};
use cassie_domain::{dto::{{model}}_dto::{{model_name}}DTO, entity::PageData, request::{{model_name}}Query};
use validator::Validate;


//{{model_name}}分页条件查询
pub async fn page(arg: Option<Query<{{model_name}}Query>>) -> impl IntoResponse {
    let service = APPLICATION_CONTEXT.get::<{{model_name}}Service>();
    let arg = arg.unwrap();
    let vo = service
        .page(
            &arg,
            PageData {
                page_no: arg.page().clone(),
                page_size: arg.limit().clone(),
            },
        )
        .await;
    RespVO::from_result(&vo).resp_json()
}
//{{model_name}}没有分页条件查询
pub async fn list(arg: Option<Query<{{model_name}}Query>>) -> impl IntoResponse {
    let service = APPLICATION_CONTEXT.get::<{{model_name}}Service>();
    let arg = arg.unwrap();
    let vo = service.list(&arg).await;
    RespVO::from_result(&vo).resp_json()
}
//{{model_name}}根据id获取
pub async fn get_by_id(Path(id): Path<String>) -> impl IntoResponse {
    let service = APPLICATION_CONTEXT.get::<{{model_name}}Service>();
    let dto = service.get(id).await;
    RespVO::from_result(&dto).resp_json()
}
//{{model_name}}根据id删除
pub async fn delete(Path(id): Path<String>) -> impl IntoResponse {
    let service = APPLICATION_CONTEXT.get::<{{model_name}}Service>();
    service.del(&id).await;
    RespVO::from(&"删除成功".to_string()).resp_json()
}
//{{model_name}}新增
pub async fn save(Json(arg): Json<{{model_name}}DTO>) -> impl IntoResponse {
    let service = APPLICATION_CONTEXT.get::<{{model_name}}Service>();
    let mut entity = arg.into();
    let vo = service.save(&mut entity).await;
    RespVO::from_result(&vo).resp_json()
}
//{{model_name}}修改
pub async fn edit(Json(arg): Json<{{model_name}}DTO>) -> impl IntoResponse {
    let service = APPLICATION_CONTEXT.get::<{{model_name}}Service>();
    let id = arg.id().clone();
    let mut entity = arg.into();
    service
        .update_by_id(id.unwrap().to_string(), &mut entity)
        .await;
    RespVO::from(&"更新成功".to_string()).resp_json()
}
//{{model_name}}初始化路由
pub fn init_router() -> Router {
    Router::new()
        .route("/{{model}}", get(page).post(save).put(edit))
        .route("/{{model}}/list", get(list))
        .route("/{{model}}/:id", get(get_by_id).delete(delete))
}